package com.fc.test.service;

import java.util.*;
import java.util.Map.Entry;

import com.alibaba.fastjson.JSONObject;
import com.fc.test.mapper.auto.EntInfoMapper;
import com.fc.test.model.auto.*;
import com.fc.test.model.vo.EntVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.util.StrUtil;
import com.fc.test.common.base.BaseService;
import com.fc.test.common.support.ConvertUtil;
import com.fc.test.mapper.auto.EntJobMapper;
import com.fc.test.model.custom.Tablepar;
import com.fc.test.util.SnowflakeIdWorker;
import com.fc.test.util.StringUtils;

/**
 * 企业职位信息表 EntJobService
 * @Title: EntJobService.java 
 * @Package com.fc.test.service 
 * @author DrRic_自动生成
 * @email drhyz@qq.com
 * @date 2020-07-05 17:21:40  
 **/
@Service
public class EntJobService implements BaseService<EntJob, EntJobExample>{
	@Autowired
	private EntJobMapper entJobMapper;
	@Autowired
	private EntInfoMapper entInfoMapper;
      	      	   	      	      	      	      	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<EntJob> list(Tablepar tablepar,EntJob record){
	        EntJobExample testExample=new EntJobExample();
	        if(record.getEid()!=null){
				testExample.createCriteria().andEidEqualTo(record.getEid());
				testExample.setOrderByClause("jid ASC");
				if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
					testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
				}
				PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
				List<EntJob> list= entJobMapper.selectByExample(testExample);
				PageInfo<EntJob> pageInfo = new PageInfo<EntJob>(list);
				return  pageInfo;
			}
		 	return new PageInfo<>();
	 }

	public PageInfo<EntJob> listForSys(Tablepar tablepar, EntJob record){
		EntJobExample testExample=new EntJobExample();
		testExample.createCriteria().andLikeQuery(record);
		testExample.setOrderByClause("jid ASC");
		if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
			testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
		}
		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		List<EntJob> list= entJobMapper.selectByExample(testExample);
		PageInfo<EntJob> pageInfo = new PageInfo<EntJob>(list);
		return  pageInfo;
	}

	/**
	 * 模糊查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	public List<EntVO> search(String keyword){
//		EntJobExample testExample=new EntJobExample();
//		EntJob record = new EntJob();
		//模糊查询支持查询以下三种类型
//		record.setName(keyword);
//		record.setDuty(keyword);
//		record.setReq(keyword);

//		testExample.createCriteria().andLikeQuery(record);
//		testExample.setOrderByClause("jid ASC");
//		if(StrUtil.isNotEmpty(tablepar.getOrderByColumn())) {
//			testExample.setOrderByClause(StringUtils.toUnderScoreCase(tablepar.getOrderByColumn()) +" "+tablepar.getIsAsc());
//		}
//		PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
		List<EntVO> result = new ArrayList<>();
		List<EntJob> list= entJobMapper.mhSearch(keyword);
		for (EntJob entJob : list){
			EntVO entVO = new EntVO();
			entVO.setEid(entJob.getEid());
			entVO.setHc(entJob.getHc());
			entVO.setJid(entJob.getJid());
			entVO.setSalary(entJob.getSalary());
			entVO.setJobLocation(entJob.getLocation());
			entVO.setJobType(entJob.getType());
			entVO.setStatus(entJob.getStatus());
			entVO.setDuty(entJob.getDuty());
			entVO.setName(entJob.getName());
			entVO.setUrgent(entJob.getUrgent());
			entVO.setReq(entJob.getReq());
			entVO.setTime(entJob.getTime());
			result.add(entVO);
		}
		Set<EntInfo> entInfoList = new HashSet<>();
		for(EntJob tmp : list){
			EntInfo tmp1 = entInfoMapper.selectByPrimaryKey(tmp.getEid());
			if(tmp1 != null){
				entInfoList.add(tmp1);
			}
			else {
				EntInfo rec = new EntInfo();
				rec.setEid(tmp.getEid());
				entInfoList.add(rec);
			}
		}
		for (EntVO r : result){
			for(EntInfo entInfo : entInfoList){
				if(r.getEid().equals(entInfo.getEid())){
					r.setAuditFile(entInfo.getAuditFile());
					r.setContact(entInfo.getContact());
					r.setEname(entInfo.getEname());
					r.setIntro(entInfo.getIntro());
					r.setEntLocation(entInfo.getLocation());
					r.setLogo(entInfo.getLogo());
					r.setEntType(entInfo.getType());
					r.setScale(entInfo.getScale());
					r.setViolationTimes(entInfo.getViolationTimes());
				}
			}
		}

		return  result;
	}

	@Override
	public int deleteByPrimaryKey(String ids) {
					
			Integer[] integers = ConvertUtil.toIntArray(",", ids);
			List<Integer> stringB = Arrays.asList(integers);
			EntJobExample example=new EntJobExample();
			example.createCriteria().andJidIn(stringB);
			return entJobMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public EntJob selectByPrimaryKey(String id) {
				
			Integer id1 = Integer.valueOf(id);
			return entJobMapper.selectByPrimaryKey(id1);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(EntJob record) {
		return entJobMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(EntJob record) {
				
		record.setJid(null);
		
				
		return entJobMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(EntJob record, EntJobExample example) {
		
		return entJobMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(EntJob record, EntJobExample example) {
		
		return entJobMapper.updateByExample(record, example);
	}

	@Override
	public List<EntJob> selectByExample(EntJobExample example) {
		
		return entJobMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(EntJobExample example) {
		
		return entJobMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(EntJobExample example) {
		
		return entJobMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param entJob
	 * @return
	 */
	public int checkNameUnique(EntJob entJob){
		EntJobExample example=new EntJobExample();
		example.createCriteria().andNameEqualTo(entJob.getName());
		List<EntJob> list=entJobMapper.selectByExample(example);
		return list.size();
	}

	public int updatePositionByEidAndJid(EntJob entJob){
		return entJobMapper.updatePositionByEidAndJid(entJob);
	}


}
